package com.example.kingdee.service;

import com.example.kingdee.KingdeeCreate.CreateBill;
import com.example.kingdee.KingdeeCreate.CreateBillFactory;
import com.example.kingdee.KingdeeCreate.dto.CreateInDto;
import com.example.kingdee.KingdeeCreate.impl.CreateProBillImpl;
import com.example.kingdee.exception.ProcedureException;
import com.example.kingdee.kingdeeBridge.KingdeeImplFactory;
import com.example.kingdee.kingdeeBridge.PushDownFactory;
import com.example.kingdee.kingdeeBridge.common.RequestResult;
import com.example.kingdee.kingdeeBridge.dto.BillWriteInDto;
import com.example.kingdee.kingdeeBridge.dto.PushDto;
import com.example.kingdee.kingdeeBridge.kingdeeAbs.FolderKingdee;
import com.example.kingdee.kingdeeBridge.kingdeeImpl.BillImpl;
import com.example.kingdee.kingdeeBridge.kingdeeImpl.impl.PpBOMChangeImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class KingdeeService {

    @Autowired
    private KingdeeImplFactory kingdeeImplFactory;

    @Autowired
    private PushDownFactory pushDownFactory;

    @Autowired
    private CreateBillFactory createBillFactory;

    @Autowired
    private CreateProBillImpl createProBill;

    @Autowired
    private PpBOMChangeImpl ppBOMChange;

    public RequestResult billWriteIn(BillWriteInDto billWriteInDto) {
        //获取单据实现类
        BillImpl billImpl = kingdeeImplFactory.createReport(billWriteInDto.getBillType());
        //填充源单和目标单表单id
        PushDto pushDto = pushDownFactory.createPushDto(billWriteInDto);
        //组合实现类和抽象类
        FolderKingdee folderKingdee = new FolderKingdee(billImpl);
        //按明细行id进行下推
        folderKingdee.addPushDtoEntryIds(pushDto, billWriteInDto.getSrcBillEntryList());
        //进行单据下推
        RequestResult pushDownResult = folderKingdee.pushDown(pushDto);
        //查询目标单据信息
        Map<String, String> targetInfo = folderKingdee.selectTargetBillEntry(String.valueOf(pushDownResult.getId()));
        //修改目标单据信息
        RequestResult result = folderKingdee.updateTargetBillEntry(pushDownResult.getId(), billWriteInDto, targetInfo);
        //提交审核订单
        try {
            folderKingdee.auditTargetBill(result.getNumber());
        }catch (ProcedureException e){
            folderKingdee.deleteTargetBill(String.valueOf(pushDownResult.getId()));
            throw e;
        }
        //审核后的操作
        folderKingdee.postPosition(result.getNumber());
        return result;
    }

    public HashMap<String, Object> billCreateIn(CreateInDto createInDto) {
        HashMap<String, Object> result = new HashMap<>();
        CreateBill createBill = createBillFactory.createObject(createInDto);
        //创建订单
        RequestResult srcBill = createBill.createSrcBill(createInDto);
        try {
            if (srcBill.getCode().equals("200")){
                //审核订单
                createBill.auditTargetBill(srcBill.getNumber());
            }else {
                //删除对应单据
                createBill.deleteTargetBill(srcBill.getId());
                throw new ProcedureException(srcBill.getMsg() + "，单据类型为：" + createInDto.getBillType() + ", 数据为：" + createInDto);
            }
        }catch (Exception e){
            //删除对应单据
            createBill.deleteTargetBill(srcBill.getId());
            throw new ProcedureException(srcBill.getMsg() + "，单据类型为：" + createInDto.getBillType() + ", 数据为：" + createInDto);
        }
        //创建订单后的操作
        createBill.postPosition(srcBill, createInDto);
        //构建返回数据
        result.put("billNO", srcBill.getNumber());
        result.put("fid", srcBill.getId());
        return result;
    }

    public HashMap<String, Object> billCreateInTest(CreateInDto createInDto) {
        HashMap<String, Object> result = new HashMap<>();
        CreateBill createBill = createBillFactory.createObject(createInDto);
        //创建订单
        RequestResult srcBill = createBill.createSrcBill(createInDto);
        try {
            if (srcBill.getCode().equals("200")){
                //审核订单
                createBill.auditTargetBill(srcBill.getNumber());
            }else {
                //删除对应单据
                createBill.deleteTargetBill(srcBill.getId());
                throw new ProcedureException(srcBill.getMsg() + "，单据类型为：" + createInDto.getBillType() + ", 数据为：" + createInDto);
            }
        }catch (Exception e){
            //删除对应单据
            createBill.deleteTargetBill(srcBill.getId());
            throw new ProcedureException(srcBill.getMsg() + "，单据类型为：" + createInDto.getBillType() + ", 数据为：" + createInDto);
        }
        //创建订单后的操作
        createProBill.postPositionTest(srcBill, createInDto);
        //构建返回数据
        result.put("billNO", srcBill.getNumber());
        result.put("fid", srcBill.getId());
        return result;
    }

    public HashMap<String, Object> ppBomTest(CreateInDto createInDto) {
        ArrayList<HashMap<String, Object>> changeBomList = ppBOMChange.selectTargetBillEntryList(createInDto.getBillType());
        ArrayList<HashMap<String, Object>> result = new ArrayList<>();
        for (int i = 0; i < changeBomList.size(); i++) {
            if (i <= 11){
                result.add(changeBomList.get(i));
//                HashMap<String, Object> changeBom = changeBomList.get(i);
//                if (changeBom.get("changeType").toString().equals("3")){
//                    changeBom.put("changeType", "4");
//                }
            }
        }
        RequestResult requestResult1 = ppBOMChange.updateTargetBillEntryList(createInDto.getBillType(), result);
        return null;
    }
}













